package com.kt.app.ui.components

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.sp
import com.kt.app.R

@Composable
fun PlusText(modifier: Modifier = Modifier) {
    val context = LocalContext.current
    Column {
        Text(
            text = stringResource(R.string.app_text),
            color=  Color.Black,
//        fontSize = TextUnit(20f, TextUnitType.Sp),
            fontSize = 16.sp,
            fontWeight = FontWeight.Bold,
            fontFamily = FontFamily.Monospace,
//        textDecoration = TextDecoration.Underline, //下划线
//        textDecoration = TextDecoration.combine(listOf(TextDecoration.Underline, TextDecoration.LineThrough)), // 合并使用
            textAlign = TextAlign.Center,
            overflow = TextOverflow.Ellipsis,
            maxLines = 1,
            modifier = modifier,
            style = TextStyle(
                background = Color.White,
                shadow = Shadow(
                    color = Color.Red,
                    offset = Offset(5f, 5f),
                    blurRadius = 10f
                )
            )
        )
        ClickableText(text = buildAnnotatedString {
            withStyle(style = SpanStyle(
                color = Color.Blue,
                fontSize = 20.sp
            )
            ){
                append("Text")
            }
            withStyle(style = SpanStyle(
                color = Color.Red,
                fontSize = 20.sp
            )){append("ClickableText")}
        }) {
            println("ClickableText $it")
            Toast.makeText(context, "ClickableText $it", Toast.LENGTH_SHORT).show()
        }
    }

}

@Preview(showBackground = true, widthDp = 300, heightDp = 150)
@Composable
private fun PlusTextPreview() {
//    PlusText()
    TextPlus()
}

@Composable
fun TextPlus(modifier: Modifier = Modifier) {
    val annotatedString = buildAnnotatedString {
        append("点击登录代表您知悉和同意")

        //往字符串中添加一个注解，直到遇到 pop() 。tag 为注解标识，annotation 为传递内容
        pushStringAnnotation("protocol", annotation = "https://docs.bughub.icu/compose")
        withStyle(style = SpanStyle(Color.Blue)) {
            append("用户协议")
        }
        pop()

        append("和")

        pushStringAnnotation("privacy", annotation = "https://randywei.gitee.com")
        withStyle(style = SpanStyle(Color.Blue)) {
            append("隐私政策")
        }
        pop()
    }

    ClickableText(
        annotatedString, onClick = { offset ->
            //从字符串中查找注解
            annotatedString.getStringAnnotations("protocol", start = offset, end = offset)
                .firstOrNull()?.let { annotation ->
                    Log.d("TextSample", "点击了用户协议：${annotation.item}")
                }

            annotatedString.getStringAnnotations("privacy", start = offset, end = offset)
                .firstOrNull()?.let { annotation ->
                    Log.d("TextSample", "点击了隐私政策：${annotation.item}")
                }
        }
    )
}